package info.pinlab.pinplayer.model.task;

import info.pinlab.pinplayer.cache.CachedValue;
import info.pinlab.pinplayer.model.display.Display;
import info.pinlab.pinplayer.model.display.TextDisplay;
import info.pinlab.util.HashCodeUtil;

import java.util.ArrayList;
import java.util.List;


public class MultichoiceTask extends AbstractTask {
	List<CachedValue<Display>> choices = new ArrayList<CachedValue<Display>>();
	
	public MultichoiceTask(){
		super.isResponsable = true;
	}
	
	public MultichoiceTask addChoice(String choice){
		return addChoice(new TextDisplay(choice));
	}
	
	public MultichoiceTask addChoice(Display choice){
		CachedValue<Display> disp = CachedValue.getCacheWrapperForValue(choice, Display.class); 
		choices.add(disp);				
//		hash = 0;
		return this;
	}
	
	/**
	 * @return number of choices
	 */
	public int getChoiceN(){
		return choices.size();
	}
	
	public Display getChoice(int ix){
		return choices.get(ix).getValue();
	}
	
	
	@Override
	public boolean equals(Object obj){
		if(!(obj instanceof MultichoiceTask))
			return false;
		MultichoiceTask other = (MultichoiceTask)obj;
		if(!super.equals(other))
			return false;
		
		int sz = choices.size();
		if(sz != other.choices.size())
			return false;
		for (int i = 0 ; i < sz ; i++){
			if(!(this.choices.get(i).getValue().equals(other.choices.get(i).getValue()))){
				return false;
			}
		}
		return true;
	}
	
	@Override
	public int hashCode(){
		int hash = HashCodeUtil.hash(373, super.hashCode());
		for(CachedValue<? extends Display> choice : choices){
			hash = HashCodeUtil.hash(hash, choice);
		}
		return hash;
	}
}
